package com.situ.struts.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.situ.struts.entity.User;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();

    public String login() {
        System.out.println("login(). ");
        if ("zhangsan".equals(user.getName()) && "123".equals(user.getPassword())) {
            // 登录成功
            ActionContext actionContext = ActionContext.getContext();
            Map<String, Object> sessionMap = actionContext.getSession();
            sessionMap.put("user", user);
            return  "loginSuccess";
        }

        return "loginFail";
    }

    public String list() {
        User user1 = new User(1, "zhangsan1", "123");
        User user2 = new User(2, "zhangsan2", "123");
        User user3 = new User(3, "zhangsan3", "123");
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("list", list);
        return "list";
    }

    @Override
    public User getModel() {
        return user;
    }
}
